<?php

class FormstatusesController extends AppController{
    var $name='Formstatuses';
    var $paginate=array(
        'limit'=>5
    );

    function  beforeFilter() {
            parent::beforeFilter();
            $this->Auth->autoRedirect = false;
           
            $this->set('active',1);
        }

    function showForms($hideClosed=null,$formId=null){

        switch ($this->Auth->user('group_id')){
            case 1:case 2:case 3:$conditions=array();break;
            case 4:                
                $conditions=array('Formstatus.Dmdformstatus_id'=>array(3,4));
                break;
            case 5:                
                $conditions=array('OR'=>array(
                    'Formstatus.demander_id'=>$this->Auth->user('id'),
                    'Dmdform.manager_id'=>  $this->Auth->user('id')
                ));
                break;
            case 7:
                $conditions=array('Formstatus.user_id'=>$this->Auth->user('id'));
                break;            
        }
        if(!empty($hideClosed) && $hideClosed!=0){
            $closed=array('Formstatus.dmdformstatus_id !='=>10);
            $conditions=array_merge($conditions,$closed);
        }
        if(!empty($formId)){
            $form=array('Formstatus.dmdform_id'=>$formId);
            $conditions=array_merge($conditions,$form);
        }
       $conditions=array_merge($conditions,$this->_constructFilter());
       //debug($conditions);
       $this->paginate=array(
           'limit'=>20,
           'conditions'=>$conditions,
                    'order'=>array('Formstatus.created DESC')
       );
            $forms=  $this->paginate('Formstatus');
        $this->set('forms',$forms);
//            $this->set('forms',  $this->Formstatus->find('all',array(
//                    'conditions'=>$conditions,
//                    'order'=>array('Formstatus.created DESC')
//                )));
    }

    function showDmdformDetails($DmdId,$typeId){
        if($DmdId){
            $modules=$this->Formstatus->Dmdform->getModulesGetActions($typeId);
            $titles=$this->Formstatus->Dmdform->getModuleMenuNames($typeId);
            $this->set('DmdId',$DmdId);
            $this->set(compact('modules','titles'));
        }
    }

    function getUserDetails($DmdId){
        $this->set('Dmdform',  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            )));
    $this->set('statuses', $this->Formstatus->User->Status->find('list'));
    $this->set('users',$this->Formstatus->User->find('list',array('fields'=>array('UsName'))));
    }
    
    function getCompanyDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            $company=array();
            if($Dmdform['Dmdform']['company_id']){
            $company=$this->Formstatus->User->Company->find('first',array(
                'recursive'=>-1,
                'conditions'=>array(
                    'Company.id'=>$Dmdform['Dmdform']['company_id']
                )
            ));
            $Dmdform['Company']=$company['Company'];}
            $this->set('Dmdform',$Dmdform);
            $this->set('countries',$this->Formstatus->Dmdform->Company->Country->find('list'));
    }
    
    function getManagerDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            $manager=array();
            if($Dmdform['Dmdform']['manager_id']){
            $manager=$this->Formstatus->User->find('first',array(
                'recursive'=>-1,
                'conditions'=>array(
                    'User.id'=>$Dmdform['Dmdform']['manager_id']
                )
            ));          
            $Dmdform['Manager']=$manager['User'];}
            $this->set('statuses', $this->Formstatus->User->Status->find('list'));
            $this->set('Dmdform',$Dmdform);
    }
    
    function getBackupDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            $backup=array();
            if($Dmdform['Dmdform']['backup_id']){
            $backup=$this->Formstatus->User->find('first',array(
                'recursive'=>-1,
                'conditions'=>array(
                    'User.id'=>$Dmdform['Dmdform']['backup_id']
                )
            ));
            $Dmdform['Backup']=$backup['User'];}
            $this->set('statuses', $this->Formstatus->User->Status->find('list'));
            $this->set('Dmdform',$Dmdform);
            $this->set('users',$this->Formstatus->User->find('list'));
    }
    
    function getManagerValidationDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            
            $this->set('Dmdform',$Dmdform);
    }
    
    function getPcDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            
            $this->set('Dmdform',$Dmdform);
    }
    
    function getNotesAndExecution($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            
            $this->set('Dmdform',$Dmdform);
    }
    
    
    function getToolsDetails($DmdId){
            $t=unserialize($this->Formstatus->Dmdform->getToolAdminArray($DmdId));
             
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            $this->set('tools',  $this->Formstatus->User->Jobprofile->Tool->find('list'));
            $this->set('toolAdmins',  $this->Formstatus->User->Jobprofile->Tool->find('list',array('fields'=>array('administrator'))));
            $this->set('users',  $this->Formstatus->User->find('list',array('fields'=>array('UsName'))));
            $this->set('Dmdform',$Dmdform);
            $this->set('ta',$t);
    }
    
    function getJobsDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            empty($Dmdform['Dmdform']['user_id'])?$uid=0:$uid=$Dmdform['Dmdform']['user_id'];
            if($uid>0){
               
            }
            
            
            $this->set('Dmdform',$Dmdform);
            $this->set('uid',$uid);
            $this->set('ujps',$this->Formstatus->User->getJobProfiles($uid));
            $this->set('jf',$this->Formstatus->User->Jobfunction->find('list'));
            $this->set('jps',$this->Formstatus->User->Jobprofile->find('list'));
            $this->set('users',$this->Formstatus->User->find('list',array('fields'=>array('UsName'))));
    }
    function getBranchDetails($DmdId){
            $Dmdform=  $this->Formstatus->find('first',array(
                'conditions'=>array(
                    'dmdform_id'=>$DmdId
                )
            ));
            empty($Dmdform['Dmdform']['user_id'])?$uid=0:$uid=$Dmdform['Dmdform']['user_id'];
            if($uid>0){
               
            }
            
            
            $this->set('Dmdform',$Dmdform);
            $this->set('uid',$uid);
            $this->set('ujps',$this->Formstatus->User->getJobProfiles($uid));
            $this->set('br',$this->Formstatus->User->Powerdelegation->Workflow->find('list',array('fields'=>array('branch'))));
            $this->set('costc',$this->Formstatus->User->Powerdelegation->Workflow->find('list',array('fields'=>array('costcenter'))));
            
    }

    function showStatusChange($Dmdid,$formId){
        $data=array();
        if($this->data){

            if (!in_array($this->Formstatus->getStatusTitle($formId), $this->_getStatusByGroup($this->Auth->user('group_id')))){
                $this->Session->setFlash(__('you can not modify this status',true));
                //$this->Sendmail->newMail($this->data['Formstatus']['statuses'],$this->Formstatus->Dmdform->getCode($Dmdid),$Dmdid);
             $this->redirect(array('action'=>'showForms'));
            }
            if ($this->data['Formstatus']['statuses']==10){
                $data['Dmdform']['closing_date']=date('Y-m-d H:i:s');
                $data['Formstatus']['closing_date']=date('Y-m-d H:i:s');
                $data['Formstatus']['days_process']=$this->Gestdate->getNbOpenDays($this->Formstatus->getCreation($formId),date('Y-m-d H:i:s'));
            }
        
    
            if ($this->data['Formstatus']['statuses']==4){
                $t=unserialize($this->Formstatus->Dmdform->getToolAdminArray($Dmdid));
               // debug($t);
                $Dmder=$this->Auth->user('id');
                $DmderA=null;
                foreach ($t as $tkey=>$tval){
                     $toolAdminId=$this->Formstatus->User->Tool->find('list',array(
                             'conditions'=>array('Tool.id'=>$tkey),
                             'fields'=>array('administrator')
                        ));
                     $toolAdminId=$toolAdminId[$tkey];
                     if($Dmder==$toolAdminId){
                         $t[$tkey]=1;
                         $DmderA='isToolAdmin';
                     }
                }
                
               if($DmderA=='isToolAdmin'){
                   if(in_array(0, $t)){
                       $data['Dmdform']['id']=$Dmdid;
                       $data['Dmdform']['tool_admin']=serialize($t);
                         if($this->Formstatus->Dmdform->save($data)){
                             $this->Sendmail->msgUser=$this->data['Formstatus']['msg_mail'];
                            $this->Session->setFlash(__('status not modified, waiting for other Tool Admin. !',true));
                            $this->Sendmail->newMail($this->data['Formstatus']['statuses'],$this->Formstatus->Dmdform->getCode($Dmdid),$Dmdid, $this->Formstatus->Dmdform->Dmdformtype->getName($this->Formstatus->Dmdform->getTypeId($Dmdid)));
                            $this->redirect(array('action'=>'showForms'));
                        }
                   }else{
                       $data['Formstatus']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];
                       $data['Formstatus']['id']=$formId;
                       $this->Formstatus->save($data);
                       $data['Dmdform']['id']=$Dmdid;
                       $data['Dmdform']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];
                       $data['Dmdform']['tool_admin']=serialize($t);
                         if($this->Formstatus->Dmdform->save($data)){
                            $this->Session->setFlash(__('status modified for all Tool Admin. !',true));
                            $this->Sendmail->msgUser=$this->data['Formstatus']['msg_mail'];
                            $this->Sendmail->newMail($this->data['Formstatus']['statuses'],$this->Formstatus->Dmdform->getCode($Dmdid),$Dmdid, $this->Formstatus->Dmdform->Dmdformtype->getName($this->Formstatus->Dmdform->getTypeId($Dmdid)));
                            $this->redirect(array('action'=>'showForms'));
                        }
                   }               
               }else{
                   $data['Formstatus']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];
                       $data['Formstatus']['id']=$formId;
                       $this->Formstatus->save($data);
                       $data['Dmdform']['id']=$Dmdid;
                       $data['Dmdform']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];
                         if($this->Formstatus->Dmdform->save($data)){
                            $this->Session->setFlash(__('status modified for all Tool Admin. !',true));
                            $this->Sendmail->msgUser=$this->data['Formstatus']['msg_mail'];
                            $this->Sendmail->newMail($this->data['Formstatus']['statuses'],$this->Formstatus->Dmdform->getCode($Dmdid),$Dmdid, $this->Formstatus->Dmdform->Dmdformtype->getName($this->Formstatus->Dmdform->getTypeId($Dmdid)));
                            $this->redirect(array('action'=>'showForms'));
                        }
               }
                
            }else{
                $data['Formstatus']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];
                $data['Formstatus']['id']=$formId;
                $this->Formstatus->save($data);
                $data=array();
                $data['Dmdform']['dmdformstatus_id']=$this->data['Formstatus']['statuses'];

                $data['Dmdform']['id']=$Dmdid;
                if($this->Formstatus->Dmdform->save($data)){
                    $this->Sendmail->msgUser=$this->data['Formstatus']['msg_mail'];
                    $this->Session->setFlash(__('status modified',true));
                    $this->Sendmail->newMail($this->data['Formstatus']['statuses'],$this->Formstatus->Dmdform->getCode($Dmdid),$Dmdid, $this->Formstatus->Dmdform->Dmdformtype->getName($this->Formstatus->Dmdform->getTypeId($Dmdid)));
                 $this->redirect(array('action'=>'showForms'));
                }
            }
        }
        $this->set('Dmdid',$Dmdid);$this->set('formId',$formId);

        $this->set('statuses', $this->_getStatusByGroup($this->Auth->user('group_id')));
    }

    function _getStatusByGroup($groupId){
        switch ($groupId):
            case 1:case 2:
                return $this->Formstatus->Dmdformstatus->find('list');
                break;
            case 3:
                return $this->Formstatus->Dmdformstatus->find('list',array('conditions'=>array('id'=>array(5,6,9))));
                break;
            case 4:
                return $this->Formstatus->Dmdformstatus->find('list',array('conditions'=>array('id'=>array(3,4))));
                break;
            case 5:
                return $this->Formstatus->Dmdformstatus->find('list',array('conditions'=>array('id'=>array(7,8))));
                break;
            case 7:
                return array();
                break;
        endswitch;
    }

    function deleteForm($formId,$dmdId){
        if($this->Formstatus->delete($formId)){
            if($this->Formstatus->Dmdform->delete($dmdId)){
                $this->Formstatus->Dmdform->Action->deleteAll(array('dmdform_id'=>$dmdId));
               $this->Session->setFlash(__('Form Deleted',true));
               $this->redirect(array('action'=>'showForms'));
            }
            
        }
    }

    function filterForms(){
        $types=$this->Formstatus->Dmdformtype->getTypes();
        $statuses=$this->Formstatus->Dmdformstatus->getStatuses();
        $users=$this->Formstatus->User->find('list',array(
            'conditions'=>array(
                'User.id'=>  $this->Formstatus->find('list',array('fields'=>'Formstatus.user_id','group'=>'Formstatus.user_id'))
            ),
            "fields"=>array('UsName')
        ));
        $demanders=$this->Formstatus->User->find('list',array(
            'conditions'=>array(
                'User.id'=>  $this->Formstatus->find('list',array('fields'=>'Formstatus.demander_id','group'=>'Formstatus.demander_id'))
            ),
            "fields"=>array('UsName')
        ));
    $this->set(compact('types','statuses','users','demanders'));
    }

    function _constructFilter(){
        $ff=array();
        //debug($this->data);
        if ($this->data){
            $f=array();$d=$this->data['Formstatus'];
            if(strlen($d['DmdformCode'])>0){$f['Dmdform.code LIKE']='%'.$d['DmdformCode'].'%';}
            if(strlen($d['dmdforms_dmdformtype_id'])>0){$f['dmdforms_dmdformtype_id']=$d['dmdforms_dmdformtype_id'];}
            if(strlen($d['dmdformstatus_id'])>0){$f['Formstatus.dmdformstatus_id']=$d['dmdformstatus_id'];}
            if(strlen($d['user_id'])>0){$f['Formstatus.user_id']=$d['user_id'];}
            if(strlen($d['demander_id'])>0){$f['Formstatus.demander_id']=$d['demander_id'];}
            if(strlen($d['form_date_begin'])>0 && strlen($d['form_date_end'])==0){$f['Dmdform.form_date >=']=$d['form_date_begin'];}
            if(strlen($d['form_date_end'])>0 && strlen($d['form_date_begin'])==0){$f['Dmdform.form_date <=']=$d['form_date_end'];}
            if(strlen($d['form_date_end'])>0 && strlen($d['form_date_begin'])>0){$f['Dmdform.form_date BETWEEN ? AND ?']=array($d['form_date_begin'],$d['form_date_end']);}
            if(!empty($f)){$ff['And']=$f;}
           return $ff;
           
        }else{
            return array();
        }
    }

    function showFormStatus($formId){
        $this->set('form',  $this->Formstatus->find('first',array(
            'conditions'=>array('Formstatus.id'=>$formId),
            'recursive'=>-1
        )));
        if($this->data){
            $this->data['Formstatus']['id']=$formId;
            if($this->Formstatus->save($this->data)){
                $this->Session->setFlash(__('Form Status changed',true));
                $this->redirect(array('action'=>'showForms'));
            }else{
                $this->Session->setFlash(__('Form Status change failed',true));
                $this->redirect(array('action'=>'showForms'));
            }
        }
    }
    
    //*
    function index() {
        $this->set('active',3);
		$this->Formstatus->recursive = 0;
		$this->set('formstatuses', $this->paginate());
	}

	function view($id = null) {
            $this->set('active',3);
		if (!$id) {
			$this->Session->setFlash(__('Invalid formstatus', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('formstatus', $this->Formstatus->read(null, $id));
	}

	function add() {
            $this->set('active',3);
		if (!empty($this->data)) {
			$this->Formstatus->create();
			if ($this->Formstatus->save($this->data)) {
				$this->Session->setFlash(__('The formstatus has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The formstatus could not be saved. Please, try again.', true));
			}
		}
		$dmdformtypes = $this->Formstatus->Dmdformtype->find('list');
		$dmdformstatuses = $this->Formstatus->Dmdformstatus->find('list');
		$dmdforms = $this->Formstatus->Dmdform->find('list');
		$demanders = $this->Formstatus->Demander->find('list');
		$users = $this->Formstatus->User->find('list');
		$this->set(compact('dmdformtypes', 'dmdformstatuses', 'dmdforms', 'demanders', 'users'));
	}

	function edit($id = null) {
            $this->set('active',3);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid formstatus', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Formstatus->save($this->data)) {
				$this->Session->setFlash(__('The formstatus has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The formstatus could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Formstatus->read(null, $id);
		}
		$dmdformtypes = $this->Formstatus->Dmdformtype->find('list');
		$dmdformstatuses = $this->Formstatus->Dmdformstatus->find('list');
		$dmdforms = $this->Formstatus->Dmdform->find('list');
		$demanders = $this->Formstatus->Demander->find('list');
		$users = $this->Formstatus->User->find('list');
		$this->set(compact('dmdformtypes', 'dmdformstatuses', 'dmdforms', 'demanders', 'users'));
	}

	function delete($id = null) {
            $this->set('active',3);
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for formstatus', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Formstatus->delete($id)) {
			$this->Session->setFlash(__('Formstatus deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Formstatus was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    //*/
}
?>
